package com.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.contant.RedisKeyConstant;
import com.example.entity.SkillGoods;
import com.example.mapper.SkillGoodsMapper;
import com.example.service.SkillGoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author ccubee
 * @since 2021/8/26 21:19
 */
@Service
@RequiredArgsConstructor
public class SkillGoodsServiceImpl extends ServiceImpl<SkillGoodsMapper, SkillGoods> implements SkillGoodsService {

    private final RedisTemplate redisTemplate;
    private final SkillGoodsMapper skillGoodsMapper;

    @Override
    public void test(SkillGoods skillGoods) {
        this.lambdaUpdate().set(SkillGoods::getStockCount, skillGoods.getStockCount() -1)
                .eq(SkillGoods::getId, skillGoods.getId()).update();
    }

    @Override
//    @Scheduled(cron = "0/5 * * * * ?")
    public void syncRedis() {
        System.out.println("同步开始=====》");
        // 扫描商品 参加到redis
        // 先查询redis中秒杀存在的id
        Set<Long> keys = redisTemplate.boundHashOps(RedisKeyConstant.SKILL_GOODS_SIGN).keys();

        LambdaQueryWrapper<SkillGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(SkillGoods::getStockCount, 0).ge(SkillGoods::getNum, 0)
                .eq(SkillGoods::getStatus, 1)
                // 如果 ids不为空 查询数据库中redis以外的 否则查询全部
                .notIn((CollUtil.isNotEmpty(keys)), SkillGoods::getId, keys);
        List<SkillGoods> skillGoods = skillGoodsMapper.selectList(wrapper);
        // 秒杀存在 插入到redis中
        if (CollUtil.isNotEmpty(skillGoods)) {
            skillGoods.forEach(skillGood -> {
                if (skillGood.getStockCount() > 0) {

                    // 商品id 商品信息存储
//                    redisTemplate.boundHashOps(RedisKeyConstant.SKILL_GOODS_SIGN).put(skillGood.getId(),  BeanUtil.beanToMap(skillGood));
                    String key = RedisKeyConstant.SKILL_GOOD + skillGood.getId();
                    redisTemplate.opsForHash().putAll(key, BeanUtil.beanToMap(skillGood));
//                    redisTemplate.opsForHash().put(skillGood.getId(),skillGood.getId(), BeanUtil.beanToMap(skillGood));
                    // 将商品信息作为队列存储到redis中[1,2,3,4,5]
                    redisTemplate.boundListOps(RedisKeyConstant.SKILL_GOODS_QUEUE + skillGood.getId()).leftPushAll(convertToArray(skillGood.getStockCount(), skillGood.getId()));
                }
            });
        } else {
            // 从队列取出
            redisTemplate.boundListOps(RedisKeyConstant.SKILL_GOODS_QUEUE).rightPop();
        }
    }

    private Long[] convertToArray(Integer stockCount, Long id) {
        Long[] ids = new Long[stockCount];
        for (int i = 0; i < stockCount; i++) {
            ids[i] = id;
        }
        return ids;
    }
}
